package com.jm.action;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;

import com.jm.dao.AgentDao;
import com.jm.dao.ManagerDao;
import com.jm.dao.Utils;
import com.jm.entity.Product;
import com.jm.viewmodel.User;
import com.jm.viewmodel.ViewProduct;
import com.opensymphony.xwork2.ActionSupport;
import com.sun.net.httpserver.Authenticator.Success;

public class ManagerAction extends ActionSupport {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private File picproduct;
	private Map<String, Object> addProductMap;
	private Map<String, Object> searchProductMap;
	private Map<String, Object> editProducMap;
	private Map<String, Object> delProducMap;
	private Map<String, Object> mULMap;
	private Map<String, Object> gCanLUPersonMap;

	private Map<String, Object> getSelfUpLevelMap;
	private Map<String, Object> reqByselfUpLevelMap;

	private Map<String, Object> gAreaAgentsMap;

	private Map<String, Object> erroProducMap = new HashMap<>();

	public Map<String, Object> getgAreaAgentsMap() {
		return gAreaAgentsMap;
	}

	public void setgAreaAgentsMap(Map<String, Object> gAreaAgentsMap) {
		this.gAreaAgentsMap = gAreaAgentsMap;
	}

	public Map<String, Object> getgCanLUPersonMap() {
		return gCanLUPersonMap;
	}

	public void setgCanLUPersonMap(Map<String, Object> gCanLUPersonMap) {
		this.gCanLUPersonMap = gCanLUPersonMap;
	}

	public Map<String, Object> getGetSelfUpLevelMap() {
		return getSelfUpLevelMap;
	}

	public void setGetSelfUpLevelMap(Map<String, Object> getSelfUpLevelMap) {
		this.getSelfUpLevelMap = getSelfUpLevelMap;
	}

	public Map<String, Object> getReqByselfUpLevelMap() {
		return reqByselfUpLevelMap;
	}

	public void setReqByselfUpLevelMap(Map<String, Object> reqByselfUpLevelMap) {
		this.reqByselfUpLevelMap = reqByselfUpLevelMap;
	}

	public Map<String, Object> getmULMap() {
		return mULMap;
	}

	public void setmULMap(Map<String, Object> mULMap) {
		this.mULMap = mULMap;
	}

	public Map<String, Object> getDelProducMap() {
		return delProducMap;
	}

	public void setDelProducMap(Map<String, Object> delProducMap) {
		this.delProducMap = delProducMap;
	}

	public Map<String, Object> getEditProducMap() {
		return editProducMap;
	}

	public void setEditProducMap(Map<String, Object> editProducMap) {
		this.editProducMap = editProducMap;
	}

	public Map<String, Object> getSearchProductMap() {
		return searchProductMap;
	}

	public void setSearchProductMap(Map<String, Object> searchProductMap) {
		this.searchProductMap = searchProductMap;
	}

	public Map<String, Object> getAddProductMap() {
		return addProductMap;
	}

	public void setAddProductMap(Map<String, Object> addProductMap) {
		this.addProductMap = addProductMap;
	}

	public File getPicproduct() {
		return picproduct;
	}

	public void setPicproduct(File picproduct) {
		this.picproduct = picproduct;
	}

	public ManagerAction() {
		erroProducMap.put("msg", "必须修改图片");
	}

	public String addProduct() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		String pname = String.valueOf(mRequest.getParameter("pname"));
		String pdescribe = String.valueOf(mRequest.getParameter("pdescribe"));
		String agentprice = String.valueOf(mRequest.getParameter("agentprice"));
		String total = String.valueOf(mRequest.getParameter("total"));

		addProductMap = new HashMap<>();
		// 参数检查
		if (pname == null || pname.length() == 0 || pdescribe == null || pdescribe.length() == 0 || agentprice == null
				|| agentprice.length() == 0 || total == null || total.length() == 0) {
			addProductMap.put("result", false);
			addProductMap.put("msg", "参数错误，参数不能为空");
			this.setAddProductMap(addProductMap);
			return SUCCESS;
		} else {

			String realPath = ServletActionContext.getServletContext().getRealPath("upload/product");
			String picPath = "";
			if (picproduct != null) {
				String picName = String.valueOf(System.currentTimeMillis() + ".jpg");
				picPath = "upload/product/" + picName;
				File savefile = new File(new File(realPath), picName);
				try {
					FileUtils.copyFile(picproduct, savefile);
					// 插入产品信息
					ManagerDao mDao = new ManagerDao();
					Long result = mDao.addProduct(pname, pdescribe, picPath, Double.valueOf(total), agentprice);
					if (result != null && result > 0) {
						addProductMap.put("result", true);
					} else {
						addProductMap.put("result", false);
					}

				} catch (IOException | NumberFormatException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					addProductMap.put("result", false);
					addProductMap.put("msg", "上传图片失败，请重新提交！");
				}
			} else {
				addProductMap.put("result", false);
				addProductMap.put("msg", "上传图片失败，请重新提交！");
			}
			return SUCCESS;
		}
	}

	public String delProduct() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		String pid = String.valueOf(mRequest.getParameter("pid"));

		delProducMap = new HashMap<>();
		// 参数检查
		if (pid == null || pid.length() == 0) {
			delProducMap.put("result", false);
			delProducMap.put("msg", "参数错误，参数不能为空");
			this.setDelProducMap(delProducMap);
			return SUCCESS;
		} else {
			ManagerDao managerDao = new ManagerDao();
			int result = managerDao.deleteProduct(Long.valueOf(pid));
			if (result > 0) {
				delProducMap.put("result", true);
				this.setDelProducMap(delProducMap);
			} else {
				if (result == -1) {
					delProducMap.put("msg", "有订单的商品不能删除");
				}
				delProducMap.put("result", false);
				this.setDelProducMap(delProducMap);
			}
		}
		return SUCCESS;
	}

	public String searchAllProduct() {
		searchProductMap = new HashMap<>();
		ManagerDao mDao = new ManagerDao();
		List<ViewProduct> mList = mDao.getProducts();
		if (mList != null && mList.size() > 0) {
			for (ViewProduct p : mList) {
				searchProductMap.put(String.valueOf(mList.indexOf(p)), p);
			}
		} else {
			searchProductMap.put("msg", "无产品信息，查询失败");
		}
		this.setSearchProductMap(searchProductMap);
		return SUCCESS;
	}

	public String editProducts() {
		HttpServletRequest mRequest = ServletActionContext.getRequest();
		try {
			mRequest.setCharacterEncoding("UTF-8");
			String pname = String.valueOf(mRequest.getParameter("pname"));
			String pdescribe = String.valueOf(mRequest.getParameter("pdescribe"));
			String agentprice = String.valueOf(mRequest.getParameter("agentprice"));
			String total = String.valueOf(mRequest.getParameter("total"));
			String pid = String.valueOf(mRequest.getParameter("pid"));
			System.out.println("888888888888888888888888888888888");
			editProducMap = new HashMap<>();
			// 参数检查
			if (pname == null || pname.length() == 0 || pdescribe == null || pdescribe.length() == 0
					|| agentprice == null || agentprice.length() == 0 || total == null || total.length() == 0) {
				editProducMap.put("result", false);
				editProducMap.put("msg", "参数错误，参数不能为空");
				this.setEditProducMap(editProducMap);
				return SUCCESS;
			} else {
				String realPath = ServletActionContext.getServletContext().getRealPath("upload/product");
				String picPath = "";
				if (picproduct != null) {
					String picName = String.valueOf(System.currentTimeMillis() + ".jpg");
					picPath = "upload/product/" + picName;
					System.out.println(picPath);
					File savefile = new File(new File(realPath), picName);
					try {
						FileUtils.copyFile(picproduct, savefile);
						// 插入产品信息
						ManagerDao mDao = new ManagerDao();
						int result = mDao.editProduct(Long.valueOf(pid), pname, pdescribe, picPath,
								Double.valueOf(total), agentprice);
						if (result > 0) {
							editProducMap.put("result", true);
						} else {
							editProducMap.put("result", false);
						}

					} catch (IOException | NumberFormatException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
						editProducMap.put("result", false);
						editProducMap.put("msg", "上传图片失败，请重新提交！");
					}
				} else {
					ManagerDao mDao = new ManagerDao();
					int result = mDao.editProduct(Long.valueOf(pid), pname, pdescribe, "", Double.valueOf(total),
							agentprice);
					if (result > 0) {
						editProducMap.put("result", true);
					} else {
						editProducMap.put("result", false);
					}
				}

				this.setEditProducMap(editProducMap);
			}
		} catch (UnsupportedEncodingException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		return SUCCESS;
	}

	public String modifyUserLevel() {
		try {
			mULMap = new HashMap<>();
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			String satue = mRequest.getParameter("statue");
			String uid = mRequest.getParameter("uid");
			boolean isModify = new ManagerDao().examineLevelUp(true, Long.valueOf(uid));
			if (isModify) {
				mULMap.put("result", true);
			} else {
				mULMap.put("result", false);
			}
		} catch (Exception e) {
			// TODO: handle exception
			mULMap.put("result", "请求失败");
		}
		return SUCCESS;
	}

	public String getCanLeveUpPerson() {
		try {
			gCanLUPersonMap = new HashMap<>();
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			List<User> users = new ManagerDao().getCanLevelUp();
			if (users != null && users.size() > 0) {
				gCanLUPersonMap.put("result", users);
			} else {
				gCanLUPersonMap.put("result", 0);
				gCanLUPersonMap.put("msg", "没有查到可以升级的代理商");
			}
		} catch (Exception e) {
			gCanLUPersonMap.put("result", "请求失败");
		}
		return SUCCESS;
	}

	public String getSelfCanUpgrade() {
		try {
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			getSelfUpLevelMap = new HashMap<>();
			String uid = mRequest.getParameter("uid");
			int statue = new ManagerDao().getCanLevelUpByUserId(Long.valueOf(uid));
			if (statue >= 100 && statue < 200) {
				getSelfUpLevelMap.put("result", statue);
				getSelfUpLevelMap.put("msg", "审核中");
			} else if (statue >= 200) {
				getSelfUpLevelMap.put("result", statue);
			} else if (statue == 0) {
				getSelfUpLevelMap.put("result", -1);
			} else {
				getSelfUpLevelMap.put("result", -1);
			}
		} catch (Exception e) {
			// TODO: handle exception
			getSelfUpLevelMap.put("result", -1);
		}

		return SUCCESS;
	}

	public String requestUpgrade() {

		try {
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			reqByselfUpLevelMap = new HashMap<>();
			String uid = mRequest.getParameter("uid");
			if (Utils.isNumeric(uid)) {
				boolean isUp = new ManagerDao().requestUpgradeByUid(Long.valueOf(uid));
				if (isUp) {
					reqByselfUpLevelMap.put("result", true);
				} else {
					reqByselfUpLevelMap.put("result", false);
				}
			}

		} catch (Exception e) {
			// TODO: handle exception
			reqByselfUpLevelMap.put("result", false);
		}

		return SUCCESS;
	}

	public String getAgentsByAreaId() {

		try {
			HttpServletRequest mRequest = ServletActionContext.getRequest();
			gAreaAgentsMap = new HashMap<>();
			String uid = mRequest.getParameter("uid");
			String areaid = mRequest.getParameter("areaid");
			System.out.println("agents"+uid);
			System.out.println("areid "+areaid);
			if (Utils.isNumeric(uid) && Utils.isNumeric(areaid)) {
				List<User> result = new ManagerDao().getAgentsOfArea(Integer.valueOf(areaid), Long.valueOf(uid));
				if (result != null && result.size() > 0) {
					gAreaAgentsMap.put("result", result);
				} else {
					gAreaAgentsMap.put("result", result);
				}
			}else{
				gAreaAgentsMap.put("result", false);
			}

		} catch (Exception e) {
			// TODO: handle exception
			gAreaAgentsMap.put("result", false);
		}

		return SUCCESS;
	}

}
